﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GTAuto.DAL.DAL;
using GTAuto.DAL.Entities;
using GTAuto.Site.BackEnd.ApCode.Filters;


namespace GTAuto.Site.BackEnd.Controllers
{

     [AutoSection]
    public class DictionaryController : AdminBaseController
    {
        
        public ActionResult Brands(int? id)
        {
            ViewBag.Brands = "active";
            ViewBag.GlobalSection = "Справочники";


            if (id == null) id = 1;

            int itemsCount;
            List<GTBrand> items = DALBrands.Singleton.GetPaged((int)id, Convert.ToInt32(DALSettngs.Singleton.Get("ItemsForPageAdmin").Value), out itemsCount);

            ViewBag.TotalCount = itemsCount;
            ViewBag.CountForPage = Convert.ToInt32(DALSettngs.Singleton.Get("ItemsForPageAdmin").Value);
            ViewBag.CurrentPage = (int)id;

            return View(items);
        }

        public ActionResult BrandEdit(int? id)
        {
            ViewBag.Brands = "active";
            ViewBag.GlobalSection = "Справочники";

            GTBrand brand = null;
            brand = id == null ? new GTBrand() : DALBrands.Singleton.Get((int)id);
            ViewBag.gtBrandTypes = new SelectList(DALBrandTypes.Singleton.GetAll(), "Id", "Name");
            return View(brand);
        }

        [HttpPost]
        public ActionResult BrandEdit(GTBrand gtBrand, HttpPostedFileBase LogoPath, HttpPostedFileBase SmallLogoPath)
        {
            ViewBag.Brands = "active";
            ViewBag.GlobalSection = "Справочники";
            ViewBag.gtBrandTypes = new SelectList(DALBrandTypes.Singleton.GetAll(), "Id", "Name");

            List<string> Error = new List<string>();



            if (LogoPath != null)
            {

                string filename = Guid.NewGuid().ToString() + "." + LogoPath.FileName.Split('.')[1];

                string filePath = Path.Combine(HttpContext.Server.MapPath(Url.Content(@"/Uploads/BrendLogos/"))
               , Path.GetFileName(filename));
               
                LogoPath.SaveAs(filePath);
                gtBrand.LogoPath = filename;
            }

            if (SmallLogoPath != null)
            {
                string filename = Guid.NewGuid().ToString() + "." + SmallLogoPath.FileName.Split('.')[1];

                string filePath = Path.Combine(HttpContext.Server.MapPath(Url.Content(@"/Uploads/BrendLogos/"))
               , Path.GetFileName(filename));
                SmallLogoPath.SaveAs(filePath);
                gtBrand.SmallLogoPath = filename;
            }


            if (string.IsNullOrEmpty(gtBrand.Name))
                Error.Add("Название не может быть пустым");


            if (Error.Count == 0)
            {
                if (gtBrand.Id > 0)
                {
                    GTBrand oldBrand = DALBrands.Singleton.Get(gtBrand.Id);

                    if (string.IsNullOrEmpty(gtBrand.LogoPath))
                        gtBrand.LogoPath = oldBrand.LogoPath;

                    if (string.IsNullOrEmpty(gtBrand.SmallLogoPath))
                        gtBrand.SmallLogoPath = oldBrand.SmallLogoPath;
                    
                    DALBrands.Singleton.Update(gtBrand);
                }
                else
                {
                    gtBrand = DALBrands.Singleton.Add(gtBrand);
                }
                ViewBag.Succes = "Изменения успешно сохранены";
            }
            else
            {
                gtBrand.LogoPath = "";
                gtBrand.SmallLogoPath = "";
            }
           
             ViewBag.Errs = Error;
            

            return View(gtBrand);
        }


        public ActionResult BrandDelete(int id)
        {
            DALBrands.Singleton.Delete(id);
            return RedirectToAction("Brands");
        }

    }
}
